const cookieparser = process.server ? require('cookieparser') : undefined
export const state=()=>{
    return {
        user: null, //登录用户的信息
    }
}
export const mutations = {
    setUser(state,data){
        state.user = data
    }
}

export const actions = {
    // nuxtServerInit是nuxt一个特殊的action，在服务端渲染期间自动调用
    nuxtServerInit ({ commit }, { req }) {
        let user = null
        if ( req && req.headers.cookie ) {
          const parsed = cookieparser.parse(req.headers.cookie)
          try {
            user = JSON.parse(parsed.user)
          } catch (err) {
          }
        }
        commit('setUser', user)
    }
}